﻿using CMS.BaseEntity;
using CMS.BusinessLogic;
using CMS.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CMS.UserControl
{
    public partial class AddProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDropDownBonus();
                LoadDropDownSection();
                txtCreateDate.Text = DateTime.Now.ToString();
            }
        }

        ProductLogic pLogic = new ProductLogic();
        SectionLogic sLogic = new SectionLogic();
        CategoryLogic cLogic = new CategoryLogic();
        protected void LoadDropDownBonus()
        {
            DataTable dt = pLogic.GetListBonus();
            ddlBonus.DataSource = dt;
            ddlBonus.DataValueField = "BonusId";
            ddlBonus.DataTextField = "BonusName";
            ddlBonus.DataBind();
        }

        protected void LoadDropDownSection()
        {
            DataTable dt = sLogic.GetListSection();
            ddlSection.DataSource = dt;
            ddlSection.DataValueField = "SectionId";
            ddlSection.DataTextField = "SectionName";
            ddlSection.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Product product = new Product();
            product.Name = txtName.Text;
            product.Description = txtDescription.Text;
            product.Intro = txtIntro.Text;
            product.Manufacture = txtManufactory.Text;
            product.Guarantee = int.Parse(ddlGuarantee.SelectedValue.ToString());
            product.BonusId = int.Parse(ddlBonus.SelectedValue.ToString());
            product.CreateDate = txtCreateDate.Text;
            product.AuthorId = int.Parse(Session["UserId"].ToString());
            product.CurrentPrice = double.Parse(txtPrice.Text);
            if (chkEnable.Checked == true)
                product.Enable = true;
            else
                product.Enable = false;
            if (chkFocus.Checked == true)
                product.Focus = true;
            else
                product.Focus = false;
            product.SectionId = int.Parse(ddlSection.SelectedValue.ToString());
            product.CategoryId = int.Parse(ddlCategory.SelectedValue.ToString());
            product.Quantity = int.Parse(txtQuantity.Text);
            if (fuImage.HasFile)
            {
                string fileName = Directory.RootPath_Image + DateTime.Now.ToString("ddMMyyyy_hhmmss_tt_") + fuImage.FileName;
                string filePath = MapPath(fileName);
                fuImage.SaveAs(filePath);
                product.Image = fileName;
            }
            else
                product.Image = Directory.Default_Image;

            if (pLogic.IsInsertProduct(product))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Success, true);
            }
            else
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Fail, true);
        }

        protected void ddlSection_SelectedIndexChanged(object sender, EventArgs e)
        {
            Category cate = new Category();
            cate.SectionId = int.Parse(ddlSection.SelectedValue.ToString());
            DataTable dt = cLogic.GetListCateBySection(cate);
            ddlCategory.DataSource = dt;
            ddlCategory.DataValueField = "CategoryId";
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataBind();
        }
    }
}